﻿Imports CreativeImagesByKim
Imports System.IO
Imports System.Web.Services
Imports System.Text

Namespace Pages
    Public Class PortfolioPage
        Inherits System.Web.UI.Page

    #Region "Private Members"
        Private _MasterPage As CIMasterPage
    #End Region

    #Region "Page Methods"
        Protected Sub Page_Load(ByVal sender As Object, 
                                ByVal e As EventArgs) _
            Handles Me.Load

            ' Get the master page
            Me._MasterPage = DirectCast(Me.Master, CIMasterPage)
        End Sub
    #End Region
        
    #Region "Private Shared Methods"
        Private Shared Function BuildPortfolioHtml(ByVal imagePaths As Dictionary(Of String, String())) As String
            Dim portfolioHtml As New StringBuilder()

            For Each imageDirectory As String In imagePaths.Keys
                Dim categoryName As String = Path.GetFileName(imageDirectory)
                Dim directoryImagePaths As String() = imagePaths(imageDirectory)

                portfolioHtml.Append("<h1>" & categoryName & "</h1>")
                portfolioHtml.Append("<div id=""pnl" & categoryName & """ class=""imageRow"">")
                portfolioHtml.Append("<div id=""pnlInner" & categoryName & """ class=""single"">")
                For Each directoryImagePath As String In directoryImagePaths
                    Dim localImagePath As String = directoryImagePath.Substring(directoryImagePath.IndexOf("Images\Portfolio")).Replace("\", "/")
                    Dim localThumbPath As String = (Path.GetDirectoryName(localImagePath) & "\Thumbs\" & Path.GetFileName(localImagePath)).Replace("\", "/")

                    portfolioHtml.Append("<a href=""" & localImagePath & """ class=""lightboxLink"" rel=""lightbox[" & categoryName & "]"">")
                    portfolioHtml.Append("<img src=""" & localThumbPath & """ alt="""" />")
                    portfolioHtml.Append("</a>")
                Next
                portfolioHtml.Append("</div></div>")
            Next

            Return portfolioHtml.ToString()
        End Function
    #End Region

    #Region "Web Methods"
        <WebMethod()>
        Public Shared Function GetPortfolioHTML() As String
            Dim imageDirectories As String() = Directory.GetDirectories(AppDomain.CurrentDomain.BaseDirectory & "Images\Portfolio")
            
            Dim portfolioImageDict As New Dictionary(Of String, String())()
            For Each imageDirectory As String In imageDirectories
                portfolioImageDict.Add(imageDirectory, Directory.GetFiles(imageDirectory))
            Next

            Return BuildPortfolioHtml(portfolioImageDict)
        End Function
    #End Region
    End Class
End Namespace